package 笔试.室友;

import java.util.*;

/**
 * @author:谢君臣
 * @Date:2021/7/14-15:54
 * @version:1.0
 * @function:
 */
public class gs3 {
    private static int[] array = null;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        int N = in.nextInt();
        int[] t = new int[N];
        array = new int[N];
        for (int i = 0; i < t.length; i++) {
            t[i] = in.nextInt();
        }
        int[] p = new int[N];
        for (int i = 0; i < p.length; i++) {
            p[i] = in.nextInt();
        }
        List<product> list = new ArrayList<>();
        for (int i = 0; i < t.length; i++) {
            list.add(new product(t[i],p[i]));
        }
        list.sort(new Comparator<product>() {
            @Override
            public int compare(product o1, product o2) {
                if (o1.price==o2.price){
                    if (o2.value > o1.value)
                        return 1;
                    else return  -1;
                }else
                    return o1.price-o2.price;
            }
        });
        System.out.println(list);
        int[] res = getResult(list,T,N,0,t);

    }

    private static int[] getResult(List<product> list, int T, int N, int index, int[] t) {
        int total = 0,max=0;
        HashMap<Integer,int[]> hashMap = new HashMap<>();
        while (index<N){
            total = total + list.get(index).time;
            if (T>= total) // 如果总加速时间大于等预定时间，考虑去掉一个
            {
                if (remove(list,T,total,index,Arrays.copyOf(t,index+1))) { // 计算当前状态下最佳方案
                    int[] temp = Arrays.copyOf(array,array.length);
                    int coust = getCoust(temp,list);
                    if (hashMap.containsKey(coust)){
                        int[]temp1 = hashMap.get(coust);
                        temp = getMaxT(temp,temp1,list);
                    }
                    hashMap.put(coust,temp);
                }
            }
            index ++;
        }
        return hashMap.get(max);
    }

    private static boolean remove(List<product> list, int T,int total, int index, int[] ints) {
        array = new int[list.size()];
        Arrays.sort(ints);
        // 快速失败法则
        if (total-ints[0]<T)return false; //减去最小的总加速时间小于目标

        return false;
    }

    private static int getCoust(int[] temp, List<product> list) {
        int sum = 0;
        for (int i = 0; i < temp.length; i++) {
            if (temp[i]==0)
                sum = sum+list.get(i).price;
        }
        return sum;
    }

    private static int[] getMaxT(int[] temp, int[] temp1, List<product> list) {
        return getTime(temp,list)>= getTime(temp,list)? Arrays.copyOf(temp,temp.length):Arrays.copyOf(temp1,temp1.length);
    }

    private static int getTime(int[] temp, List<product> list) {
        int sum = 0;
        for (int i = 0; i < temp.length; i++) {
            if (temp[i]==0)
            sum = sum+list.get(i).time;
        }
        return sum;
    }

}
